﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

#endregion

namespace OrePerHour.Plugins
{
    public partial class OPH : Form
    {
        private readonly long _elapsedMilliseconds;
        private readonly Dictionary<string, int> _oreCounter = new Dictionary<string, int>();

        public OPH(Dictionary<string, int> newOreCounter, long newElapsedMiliseconds)
        {
            InitializeComponent();
            _oreCounter = newOreCounter;
            _elapsedMilliseconds = newElapsedMiliseconds;
        }

        public void CalculateOPH()
        {
            // Get total number of ore
            long totalOre = _oreCounter.Aggregate(0, (current, i) => current + i.Value);
            // Get time running
            long timerunning = _elapsedMilliseconds / (1000 * 60 * 60);
            // Get ore per hour
            if (totalOre != 0)
            {
                PrintStats(totalOre / timerunning);
            }
        }

        public void PrintStats(long oph)
        {
            listBox1.Items.Add("**** Ore Per Hour ****");
            listBox1.Items.Add("Collected:");
            foreach (KeyValuePair<string, int> i in _oreCounter)
            {
                listBox1.Items.Add(i.Value + " x " + i.Key);
            }
            listBox1.Items.Add("Total: " + oph);
            listBox1.Items.Add("**** Ore Per Hour ****");
        }

        private void OPHLoad(object sender, EventArgs e)
        {
            CalculateOPH();
        }
    }
}